import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { fetch, add, get } from './service';
import { TableListData } from './data.d';
import { message } from 'antd';
import Constants from '../../customsInfoGenerate/utils/Constants';
const R = require('ramda');

export interface StateType {
  data: TableListData;
}

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: StateType) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: StateType;
  effects: {
    fetch: Effect;
    add: Effect;
    remove: Effect;
    update: Effect;
  };
  reducers: {
    save: Reducer<StateType>;
  };
}

const Model: ModelType = {
  namespace: 'policyInsurance',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    //bizParams: {}, // 业务参数缓存
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      payload.type = 1;
      const response = yield call(fetch, payload);
      if (response.resultCode === 'ok') {
        let page = Constants.DEFAULT_PAGE;
        let list = [];
        if (response.content) {
          page = response.content.page;
          list = response.content.list;
        }
        yield put({
          type: 'save',
          payload: {
            list,
            pagination: { current: page.page, total: page.totalRow, pageSize: page.size },
          },
        });
      }
    },

    *add({ payload }, { call, put, select }) {
      const res = yield call(add, { ...R.omit(['onSuccess', 'onFail'])(payload) });

      if (res.resultCode === 'ok') {
        message.info('申请成功！');
        if (payload.onSuccess) payload.onSuccess();
      } else {
        message.error(res.errMessage);
        if (payload.onFail) payload.onFail();
      }
    },

    // *getBusinessParamsByCode({ sprCode }, { call, put, select }) {
    //   const sessionSprCode = sessionStorage.getItem(Constants.PARAMPREFIX + sprCode);
    //   if (R.isNil(sessionSprCode)) {
    //     const response = yield call(getBusinessParamsByCode, { sprCode });
    //     if (response.resultCode === 'ok') {
    //       sessionStorage.setItem(Constants.PARAMPREFIX + sprCode, JSON.stringify(response.content));
    //       yield put({
    //         type: 'setBizParams',
    //         payload: {
    //           sprCode,
    //           list: response.content,
    //         },
    //       });
    //     }
    //   } else {
    //     yield put({ type: 'setBizParams', payload: { sprCode, list: JSON.parse(sessionSprCode) } });
    //   }
    // },

    *get({ payload }, { call, put, select }) {
      const response = yield call(get, { id: payload.id });
      if (response.resultCode === 'ok') {
        if (payload.onSuccess) payload.onSuccess(response.content);
      } else {
        message.error(response.errMessage);
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    /**
     * 设置业务参数
     *
     * @param state
     * @param payload
     */
    setBizParams(state, { payload }) {
      let newState = state.bizParams;
      if (payload.sprCode != undefined) {
        newState[payload.sprCode] = payload.list;
      }
      return { ...state, bizParams: newState };
    },
  },
};

export default Model;
